package com.project.sianet.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.project.sianet.MainActivity;
import com.project.sianet.R;
import com.project.sianet.data.ActividadArrayAdapter;
import com.project.sianet.entidades.Actividad;
import com.project.sianet.util.ConnectionDetector;

public class CalendarioDetalleFragment extends Fragment {
 
	private String mTitleFrag, mSubTitleFrag;
	private List<Actividad> mActividades = new ArrayList<Actividad>();
	
    // Clase Connection detector
    ConnectionDetector cd;
	
    public CalendarioDetalleFragment() {}
    
    public static final CalendarioDetalleFragment newInstance(String titulo, String subtitle, List<Actividad> a) {
    	CalendarioDetalleFragment f = new CalendarioDetalleFragment();
    	Bundle args = new Bundle();
		args.putString("mTitleFrag", subtitle);
		args.putString("mSubTitleFrag", titulo);
		args.putParcelableArrayList("mActividades", (ArrayList<Actividad>) a);
		f.setArguments(args);
    	return f;
    }

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.calendario_detalle, null);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		// Creo la instancia de Connection detector
        cd = new ConnectionDetector(getActividad().getApplicationContext());
		
        mTitleFrag = getArguments().getString("mTitleFrag");
        mSubTitleFrag = getArguments().getString("mSubTitleFrag");
        mActividades = getArguments().getParcelableArrayList("mActividades");
		
		TextView title = (TextView)this.getActivity().findViewById(R.id.tv_title);
		TextView subtitle = (TextView)this.getActivity().findViewById(R.id.tv_subTitle);
		LinearLayout shadow = (LinearLayout)this.getActivity().findViewById(R.id.layout_shadow);
	    ListView lvActividadesDet = (ListView) getActividad().findViewById(R.id.lv_actividadesDet);

	    
		if (TextUtils.isEmpty(mTitleFrag)) {
			title.setText(mSubTitleFrag);
			shadow.setVisibility(LinearLayout.GONE);
		} else {
			subtitle.setVisibility(View.VISIBLE);
			title.setText(mTitleFrag);
			title.setMaxLines(1);
			title.setTextSize(16);
			subtitle.setText(mSubTitleFrag);
		}
		
		ActividadArrayAdapter adapter = new ActividadArrayAdapter(getActividad(), R.layout.actividad_list, mActividades);
		lvActividadesDet.setAdapter(adapter);
			
		lvActividadesDet.setOnItemClickListener(new OnItemClickListener() {
				
		    @Override
		    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		    	onSelectedActividadItemClick(parent, view, position, id);
		    }
            
		});
		
	}
	
	private void onSelectedActividadItemClick(AdapterView<?> parent, View view, int position, long id) { 
    	ActividadArrayAdapter arrayActividad = (ActividadArrayAdapter) parent.getAdapter();
    	Actividad act = arrayActividad.getItem(position);
    	Fragment newContent = ActividadDetalleFragment.newInstance(act, mTitleFrag);
    	switchFragment(newContent);
    }
	
	private void switchFragment(Fragment fragment) {
 		if (getActivity() == null)
 			return;
 		
 		if (getActivity() instanceof MainActivity) {
 			MainActivity ma = (MainActivity) getActivity();
 			ma.switchContent(fragment);
 		}
 	}
		
	private Activity getActividad(){
		return this.getActivity();
	}
	
}
